import 'package:flutter/scheduler.dart';

import '../index.dart';

class ProfileChangeNotifier extends ChangeNotifier {
  Profile get _profile => Global.profile;

  @override
  void notifyListeners() {
    Global.saveProfile(); //保存Profile变更
    super.notifyListeners(); //通知依赖的Widget更新
  }
}

class UserModel extends ProfileChangeNotifier {
  User? get user => _profile.user;

  // APP是否登录(如果有用户信息，则证明登录过)
  bool get isLogin => user != null;

  //用户信息发生变化，更新用户信息并通知依赖它的子孙Widgets更新
  set user(User? user) {
    if (user?.account != _profile.user?.account) {
      if (_profile.user?.account != null) {
        _profile.lastLogin = _profile.user?.account;
      }
      _profile.user = user;
      notifyListeners();
    }
  }
}

class ThemeModel extends ProfileChangeNotifier {
  // 获取当前主题，如果为设置主题，则默认使用蓝色主题
  MaterialColor get theme => Global.themes
      .firstWhere((e) => e.value == _profile.theme, orElse: () => Colors.blue);

  // 主题改变后，通知其依赖项，新主题会立即生效
  set theme(MaterialColor color) {
    if (color != theme) {
      _profile.theme = color[500]!.value;
      notifyListeners();
    }
  }
}

class LocaleModel extends ProfileChangeNotifier {
  // 获取当前用户的APP语言配置Locale类，如果为null，则语言跟随系统语言
  Locale? getLocale() {
    if (_profile.locale == null) return null;
    List<String> t = _profile.locale!.split("_");
    // if (t.length == 1) {
    //   return Locale(t[0]);
    // } else if (t.length == 2) {
    //   return Locale(t[0], t[1]);
    // }
    return Locale.fromSubtags(
        languageCode: t[0],
        scriptCode: t.length > 1 ? t[1] : null,
        countryCode: t.length > 2 ? t[2] : null);
  }

  // 获取当前Locale的字符串表示
  String? get locale => _profile.locale;

  // 用户改变APP语言后，通知依赖项更新，新语言会立即生效
  set locale(String? locale) {
    if (locale != _profile.locale) {
      _profile.locale = locale;
      notifyListeners();
    }
  }
}

//字体缩放
class TextScalingModel extends ProfileChangeNotifier {
  // 获取当前系统尺寸
  // double? getFontSize() {
  //   var textScaling = _profile.textScaling;
  //   return textScaling >= 1 ? (1 + textScaling) / 2 : 1;
  // }

  // 获取当前缩放比例textScaling
  num get textScaling => _profile.textScaling;

  // 当用户改变字体缩放比例时，通知依赖项目更新
  set textScaling(num textScaling) {
    if (textScaling != _profile.textScaling) {
      _profile.textScaling = textScaling;
      notifyListeners();
    }
  }
}

//夜间模式
class DarkModel extends ProfileChangeNotifier {
  ThemeMode getThemeMode() {
    return _profile.darkMode == DarkModelOption.darkModelOptionSystem.index
        ? ThemeMode.system
        : _profile.darkMode == DarkModelOption.darkModelOptionDark.index
            ? ThemeMode.dark
            : ThemeMode.light;
  }

  // 获取是否开启夜间模式
  int get darkMode => _profile.darkMode as int;

  // 当用户改变夜间模式时，通知依赖项目更新
  set darkMode(int darkMode) {
    if (darkMode != _profile.darkMode) {
      _profile.darkMode = darkMode;
      notifyListeners();
    }
  }

  var _platformBrightness =
      SchedulerBinding.instance.window.platformBrightness;

  /// 系统Dark Mode 发生变化
  void darModeChange() {
    if (_platformBrightness !=
        SchedulerBinding.instance.window.platformBrightness) {
      _platformBrightness =
          SchedulerBinding.instance.window.platformBrightness;
      notifyListeners();
    }
  }

  /// 是否是 Dark Mode
  bool isDark(BuildContext context) {
    ThemeMode _themeMode = getThemeMode();
    if (_themeMode == ThemeMode.system) {
      return MediaQuery.platformBrightnessOf(context) == Brightness.dark;
    }
    return _themeMode == ThemeMode.dark;
  }
}

//动画延时效果
class TimeDilationModel extends ProfileChangeNotifier {
  // 获取时间膨胀系数
  num get timeDilation => _profile.timeDilation;

  // 当用户改变时间膨胀时，通知依赖项目更新
  set timeDilation(num timeDilation) {
    if (timeDilation != _profile.timeDilation) {
      _profile.timeDilation = timeDilation;
      notifyListeners();
    }
  }
}
